package com.example.mapper;

import com.example.pojo.Collection;
import com.example.pojo.Comment;
import com.example.pojo.Work;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WorkMapper {
    //获取所有作品，按发布时间倒序排列，并关联用户信息
    @Select("SELECT w.*, u.username as authorName, u.name as authorRealName " +
            "FROM work w " +
            "LEFT JOIN users u ON w.user_id = u.id " +
            "ORDER BY w.publish_time DESC")
    List<Work> findAll();

    //插入作品
    @Insert("INSERT INTO work (title, description, user_id, publish_time, img_url) " +
            "VALUES (#{title}, #{description}, #{userId}, #{publishTime}, #{imgUrl})")
    void insert(Work work);

    //插入评论
    @Insert("INSERT INTO comment (user_id, work_id, username, content, publish_time) " +
            "VALUES (#{userId}, #{workId}, #{username}, #{content}, #{publishTime})")
    void insertComment(Comment comment);


    //更新作品的浏览量
    @Update("UPDATE work SET views = views + 1 WHERE id = #{id}")
    void incrementViews(Integer id);

    //更新作品的点赞数
    @Update("UPDATE work SET likes = likes + 1 WHERE id = #{id}")
    void incrementLikes(Integer id);

    //根据id获取作品
    @Select("SELECT w.*, u.username as authorName, u.name as authorRealName " +
            "FROM work w " +
            "LEFT JOIN users u ON w.user_id = u.id " +
            "WHERE w.id = #{id}")
    Work findById(Integer id);
    //根据用户Id获取作品
    @Select("SELECT w.*, u.username as authorName, u.name as authorRealName " +
            "FROM work w " +
            "LEFT JOIN users u ON w.user_id = u.id " +
            "WHERE w.user_id = #{userId}")
    List<Work> findByUserId(Integer userId);
    //获取作品的评论
    @Select("SELECT c.id, c.user_id, c.work_id, " +
            "c.username, CAST(c.content AS CHAR) as content, c.publish_time, " +
            "u.username as authorName " +
            "FROM comment c " +
            "LEFT JOIN users u ON c.user_id = u.id " +
            "WHERE c.work_id = #{workId} " +
            "ORDER BY c.publish_time DESC")
    List<Comment> findCommentsByWorksId(Integer workId);

    // 获取随机作品用于轮播图
    @Select("SELECT id, title, " +
            "CAST(description AS CHAR) as description, " +
            "CAST(img_url AS CHAR) as img_url " +
            "FROM work ORDER BY RAND() LIMIT 3")
    List<Work> findRandomWorks();
    //删除作品
    @Update("DELETE FROM work WHERE id = #{id}")
    void deleteWork(Integer id);

    @Insert("INSERT INTO collections (user_id, work_id) " +
            "VALUES (#{userId}, #{workId})")
    void insertCollection(Collection collection);

    //查看用户收藏的作品
    @Select("SELECT w.* FROM collections c " +
            "JOIN work w ON c.work_id = w.id " +
            "WHERE c.user_id = #{userId}")
    List<Collection> selectCollectionsByUserId(int userId);

    //查看用户是否收藏了该作品
    @Select("SELECT COUNT(*) FROM collections WHERE user_id = #{userId} AND work_id = #{workId}")
    boolean checkCollection(Integer userId, Integer workId);

    //删除收藏
    @Delete("DELETE FROM collections WHERE user_id = #{userId} AND work_id = #{workId}")
    void deleteCollection(Integer userId, Integer workId);
}
